<?php
/**
 * Esta clase deberia heredar de Zend_Queue_Message, el tema es que es demasiado rebuscada
 * por que intenta implementar el mensaje de entrada y el de salida de la cola en una sola clase.
 */
class Ext_Message
{

    protected $msg = null;

    protected $data = null;

    public function __construct( $msg = null )
    {
        $this->getData( $msg );
    }

    public function getData( $msg = null )
    {
        $this->setMessage( $msg );
        if ( $this->data === null ) {
            $json = new Zend_Json( );
            $this->data = $json->decode( $this->msg );
        }
        return $this->data;
    }

    public function setData( $data = null )
    {
        if ( $data !== null ) {
            $this->data = $data;
        }
    }

    public function getMessage( $data = null )
    {
        $this->setData( $data );
        $json = new Zend_Json( );
        $this->msg = $json->encode( $this->data );
        return $this->msg;
    }

    public function setMessage( $msg = null )
    {
        if ( $msg !== null ) {
            if ( $msg instanceof Ext_Queue ) {
                $msg = $msg->dequeue()->body;
            }
            $this->msg = $msg;
        }
    }

    public function __get( $columnName )
    {
        $data = $this->getData();
        return $data[ $columnName ];
    }

    public function __set( $columnName, $value )
    {
        $data = $this->getData();
        $data[ $columnName ] = $value;
        $this->setData( $data );
    }

    public function __isset( $columnName )
    {
        $data = $this->getData();
        return isset( $data[ $columnName ] );
    }
}